package com.tj.bo.user.server.dao.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.ObjectMetadata;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2020-09-22
 * Time: 15:55
 */
public class OssMoveUtil {


    public static final String keyId = "LTAI4G3YSEkC2pSWPT1mok95";

    public static final String keySecret = "hcilwgwu3Wi3XyWI4pf2tfslITFeNc";

    public static void main(String[] args) {

        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-ap-south-1.aliyuncs.com";
       // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = keyId;
        String accessKeySecret = keySecret;

        String sourceBucketName = "cashmama-pro";
        String sourceObjectName = "highmark/20200922/e8a7c2f82bae47328d1fc791fddb6ed3.txt";
        String destinationBucketName = "cashmama-pro";
        String destinationObjectName = "highmarknew/aa.txt";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建CopyObjectRequest对象。
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);

       // 设置新的文件元信息。
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("text/html");
        copyObjectRequest.setNewObjectMetadata(meta);

        // 复制文件。
        CopyObjectResult result = ossClient.copyObject(copyObjectRequest);
        System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());

        // 关闭OSSClient。
        ossClient.shutdown();

    }
}

